"""
Case Type   : datakit安装
Case Name   : 账号只能看到有权限看的菜单和页面
Create At   : 2025/4/21
Owner       : @lonely-dance
Description :
    1.创建角色,赋予插件管理权限
    2.创建用户,把新建角色分配给新用户
    3.新用户登录
    4.查看菜单页面
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.只能看到插件管理的菜单
    5.成功
History     :
"""

import os
import re
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.datakit_api.CommonApi import CommonApi


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.datakit_node = Node('Datakit_Root')
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.log.info(self.token)
        self.assertNotEqual("error", self.token)
        self.com_api = CommonApi(self.token)
        self.user_name = re.search(r'Case\d+', os.path.basename(__file__)).group()
        self.role_id = 0

    def test_base_ops(self):
        text = '----step1:创建角色,赋予插件管理权限 expect:成功----'
        self.log.info(text)
        data = {"menuIds": [5], "roleName": f"{self.user_name}", "status": "0"}
        response = self.com_api.post_system_role(data)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)
        self.log.info('----获取新角色roleId----')
        response = self.com_api.get_system_role_list(role_name=f'{self.user_name}')
        self.log.info(response.json())
        self.assertEqual(1, response.json()['total'], '角色数量异常')
        self.role_id = response.json()['rows'][0]['roleId']
        self.log.info(self.role_id)

        text = '----step2:创建账户 expect:成功----'
        self.log.info(text)
        encrypt_password = self.com_sh.encrypt_password(f'{macro.COMMON_PASSWD}')
        data = {"userName": f"{self.user_name}",
                "password": f"{encrypt_password}",
                "nickName": f"{self.user_name}", "phonenumber": "11111110090", "roleIds": self.role_id, "status": "0"}
        response = self.com_api.post_system_user(data)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

        text = '----step3:普通账户密码登录 expect:成功----'
        self.log.info(text)
        response = self.com_sh.user_login(encrypt_password, user=f"{self.user_name}")
        self.log.info(response.json())
        self.assertEqual('success', response.json()['msg'], '执行失败' + text)
        token = response.json()['token']

        text = '----step4:查看菜单页面 expect:成功'
        self.log.info(text)
        user_com_api = CommonApi(token)
        response = user_com_api.get_getRouters()
        self.log.info(response.json())
        self.assertIn('插件管理', response.text, '执行失败' + text)
        self.assertEqual(2, len(response.json()['data']), '执行失败' + text)

    def tearDown(self):
        self.log.info('----step3:清理环境----')
        self.log.info('----删除用户----')
        response = self.com_api.get_system_user_list(user_name=self.user_name)
        self.log.info(response.json())
        self.assertEqual(1, response.json()['total'])
        user_id = response.json()['rows'][0]['userId']
        response = self.com_api.delete_system_user(user_id)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '删除用户失败')
        self.log.info('----删除角色----')
        response = self.com_api.delete_system_role(self.role_id)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '删除角色失败')
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
